import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

public class demo36 {
    public static void main(String[] args) {
        //使用阻塞队列，最后消费者线程和生产者线程一定会逐渐趋于同步
        BlockingQueue<Integer> blockingQueue=new LinkedBlockingQueue<>();
        Thread producer1=new Thread(()->{
            int n=0;
            while(true){
                try {
                    blockingQueue.put(n);
//                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("producer1生产元素"+n);
                n++;
            }
        },"producer1");
        Thread producer2=new Thread(()->{
            int p=0;
            while(true){
                try {
                    blockingQueue.put(p);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("producer2生产元素"+p);
                p++;

            }
        });
        Thread consumer=new Thread(()->{
            while(true){
                int m= 0;
                try {
                    m = blockingQueue.take();
                    //构造供大于求
//                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("消费元素"+m);
            }
        },"consumer");
        producer1.start();
//        producer2.start();
        consumer.start();
    }
}
